package main

import (
	"fmt"
	"time"
)

func main() {
	// 1. 获取当前时间
	now := time.Now()
	// 2. 格式化时间
	strTime := now.Format("2006-01-02 15:04:05")

	// 3. 输出当前时间
	fmt.Println(now)
	fmt.Println(strTime)
	// 4. 输出当前时间戳
	fmt.Println(now.Unix())
	// 4.1 毫秒时间戳
	fmt.Println(now.UnixMilli())
	// 5. 格式化时间转换为时间对象
	loc, _ := time.LoadLocation("Asia/Shanghai")
	timeObj, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, loc)
	fmt.Println(timeObj.Unix())
	fmt.Printf("%v %T\n", timeObj, timeObj)
	// 6. 输出是时间的周几
	weekday := now.Weekday()
	switch weekday {
	case time.Monday:
		fmt.Println("星期一")
	case time.Tuesday:
		fmt.Println("星期二")
	case time.Wednesday:
		fmt.Println("星期三")
	case time.Thursday:
		fmt.Println("星期四")
	case time.Friday:
		fmt.Println("星期五")
	case time.Saturday:
		fmt.Println("星期六")
	case time.Sunday:
		fmt.Println("星期日")
	}
	// 7. 获取事件对象的年月日时分秒
	year := now.Year()
	month := now.Month()
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()
	fmt.Printf("%d-%d-%d %d:%d:%d\n", year, month, day, hour, minute, second)
	// 02表示宽度，如果整数的长度不够，则在前面补0
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)

	// 8. 时间戳和时间对象的转换

	timeObj2 := time.Unix(now.Unix(), 0)
	timeObj3 := time.UnixMilli(now.UnixMilli())
	fmt.Println(timeObj2, "-- 隔离 --", timeObj3)

	// 时间间隔 time.Duration
	d := time.Second * 5
	time.Sleep(d)
	fmt.Println(d)

	// 9. 一分钟后的时间对象
	timeObj4 := now.Add(1 * time.Minute)
	fmt.Println(timeObj4)

	// 10. 一分钟后的时间对象
	timeObj5 := now.Add(-1 * time.Minute)
	fmt.Println(timeObj5)

	// 两个时间对象之间的时间间隔
	d2 := timeObj4.Sub(timeObj5)
	fmt.Println(d2)
}
